import java.io.*;
import java.util.*;

public class Corpus {
	
	private class Data {
//		public String word;
		public int instances;		
//		public double p; //Probability of current word
		
		public Data(String word) {
//			this.word = word;
			this.instances = 0;
//			this.p = 0;
		}
	}
	
	//data members
	private HashMap<String, Data> dict; //dictionary of unique words and their instances
	private int S = 0; //size of total seen words
	
	/**
	 * Gets the number of unique words
	 * @return
	 */
	public int X() { return dict.size(); }
	
	/**
	 * Gets the total number of words 
	 * @return
	 */
	public int S() { return S; }
	
	/**
	 * Default CTOR
	 */
	public Corpus() {
		dict = new HashMap<String, Data>();
	}
	
	public void PRINT() {
		try {
		PrintWriter writer = new PrintWriter("OUTPUT.txt");
		
		for (String key : dict.keySet())
			writer.println(key);
		
		writer.close(); }
		catch (IOException ex){}
	}
	
	/**
	 * CTOR
	 * @param fileName
	 * @throws IOException
	 */
	public Corpus(String fileName) throws IOException{
		this();
		
		BufferedReader reader = new BufferedReader(new FileReader(fileName));
		
		String line = null;
		
		while ((line = reader.readLine()) != null) {
			String[] parsed = line.split(" ");
			
			for (String str : parsed)
				addWord(str);
		}
		
		reader.close();
	}
		
	/**
	 * Adds a new word into the corpus
	 */
	public void addWord(String word) {
		Data d = dict.get(word);
		
		if (d == null)
			d = new Data(word);
		
		++d.instances;
		++S;
		
		dict.put(word, d);
	}
			
	/**
	 * Gets the number of instances of the given word 
	 */
	public int C(String word){
		Data d=dict.get(word);
		return d == null ? 0 : d.instances;
	}
	
	/**
	 * Gets a collection of unique words in the corpus
	 * @return
	 */
	public Set<String> getUniqueWordSet() {
		return dict.keySet();
	}
}
